package com.tg.app.activity.device.add;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.tange.module.add.guide.OnApResetFinishListener;
import com.tange.module.core.wifi.scan.WiFiScanManager;

import com.tg.app.R;
import com.base.BaseActivity;
import com.tg.app.helper.ActivityHelper;
import com.tg.app.util.LogUtils;
import com.tg.appcommon.android.DeviceAddSoundConstants;
import com.tg.appcommon.android.DialogUtil;
import com.tg.appcommon.android.SoundPlayerManager;
import com.tg.data.helper.DeviceTypeHelper;

import java.util.HashMap;

public  class ApGuideActivity extends BaseActivity {

    public static  final  String TAG  =  "ApGuideActivityTAG";

    public static final String AP_GUIDE_TYPE = "ap_guide_type";
    public static final int DEVICE_RESET = 1;
    public static final int DEVICE_DOOR_LOCK = 2;
    public static final int DEVICE_LAMP_LOCK = 3;

    private int type ;

    private String deviceType;

    private NormalApGuideView normalApGuideView;

    private SoundPlayerManager soundPlayerManager;

    private final Handler uiHandler = new Handler(Looper.getMainLooper());

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.fragment_ap_guide);
        hideActionBar();
        type = getIntent().getIntExtra(AP_GUIDE_TYPE,0);

        deviceType = getIntent().getStringExtra(ApSetWifiActivityEx.EXT_DEVICE_TYPE);

        initView();

        HashMap<String,String> map = new HashMap<>();
        map.put("mode","bind_app");
        map.put("event","bind_start");
        LogUtils.uploadLog(map);

        boolean isLocationGranted = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
        if (!isLocationGranted) {
            LocationGrantGuideActivity.launch(this);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == LocationGrantGuideActivity.REQUEST_CODE) {
            if (resultCode == RESULT_CANCELED) {
                finish();
            } else if (resultCode == RESULT_OK) {
                // do nothing
            }
        }
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (WiFiScanManager.scanner().isLocationEnabled(this)){
            WifiManager mWifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            if (!mWifiManager.isWifiEnabled()){
                DialogUtil.openWifi(ActivityHelper.getActivityContext(this));
            }else {
                mWifiManager.startScan();
            }
        }else {
            DialogUtil.openGpsService(this);
        }

        boolean isLocationGranted = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;

        if (type ==  DEVICE_RESET){
            if (normalApGuideView != null) {
                normalApGuideView.showResetDeviceType();
            }
            if (isLocationGranted && normalApGuideView instanceof NormalApGuideView) {
                playSound(DeviceAddSoundConstants.WifiReset(this));
            }
        } else {
            if (isLocationGranted && normalApGuideView instanceof NormalApGuideView) {
                playSound(DeviceAddSoundConstants.WifiPower(this));
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        pauseSound();
    }

    private void pauseSound() {
        uiHandler.removeCallbacksAndMessages(null);
        if (soundPlayerManager != null) {
            soundPlayerManager.destroy();
        }
    }

    private void playSound(String url) {
        if (soundPlayerManager != null) {
            soundPlayerManager.destroy();
        }
        soundPlayerManager = SoundPlayerManager.create(this);
        uiHandler.postDelayed(() -> {
            if (soundPlayerManager != null) {
                soundPlayerManager.download(url, true);
            }
        }, 1000);
    }

    @Override
    protected void initView() {

        final RelativeLayout rootView = findViewById(R.id.root);

        {
            normalApGuideView = new NormalApGuideView(this);
            normalApGuideView.setOnApResetFinishListener(new OnApResetFinishListener() {
                @Override
                public void onFinishSetting() {
                    launchApListActivity();
                }

                @Override
                public void onHowToResetClick() {
                    playSound(DeviceAddSoundConstants.WifiReset(ApGuideActivity.this));
                }
            });
            rootView.addView(normalApGuideView, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        }
    }

    private void launchApListActivity() {
        Intent intent = new Intent(getBaseContext(), ApDeviceListActivity.class);
        intent.putExtra(ApSetWifiActivity.EXT_DEVICE_TYPE, getIntent().getStringExtra(ApSetWifiActivity.EXT_DEVICE_TYPE));
        startActivity(intent);
        finish();
    }
}
